<?php

namespace App\Store\Controllers\Wxapp;


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Wxapp\Electronicchop;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Wxapp\WarrantycertificateSetting as WxappWarrantycertificateSetting;

class WarrantycertificateSettingController extends Controller
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $list = Electronicchop::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    

    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $imgpath = $request->imgpath ?? '';

        if($imgpath==''){
            return response()->json(['code' => '001', 'msg' => '轮播图未上传']);
        }
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $data = ['imgpath'=>$imgpath,'cp_id'=>$cp_id];
        $data['create_at'] = time();
        $data['update_at'] = time();
        Electronicchop::create($data);
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 更改电子章状态
     */
    public function changestatus(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = Electronicchop::where([['id', $id],['cp_id',$cp_id]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        if($info->status==1){
            Electronicchop::where([['id', '<>',$id],['cp_id',$cp_id]])->update(['status' => 1, 'update_at' => time()]);
            Electronicchop::where([['id', $id],['cp_id',$cp_id]])->update(['status' => 2, 'update_at' => time()]);
        }else{
            
            Electronicchop::where([['id', $id],['cp_id',$cp_id]])->update(['status' => 1, 'update_at' => time()]);
        }

       
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }



    public function getsettinginfo(Request $request){
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $info=WxappWarrantycertificateSetting::where([['cp_id',$cp_id]])->first();
        
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info'=>$info]]);
    }

    public function postsetting(Request $request){

        $desc1=$request->desc1 ?? '';
        $desc2=$request->desc2 ?? '';
        
        if($desc1=='' && $desc2==''){
            return response()->json(['code' => '001', 'msg' => '无效内容']);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $info=WxappWarrantycertificateSetting::where([['cp_id',$cp_id]])->first();
        $data=$desc1?['desc1'=>$desc1]:['desc2'=>$desc2];
        
        $data['update_at']=time();
        if($info){
            WxappWarrantycertificateSetting::where([['cp_id',$cp_id]])->update($data);
        }else{
            $data['cp_id']=$cp_id;
            $data['create_at']=time();

            if(isset($data['desc1'])){
                $data['desc2']='';
            }else{
                $data['desc1']='';
            }
            
            WxappWarrantycertificateSetting::create($data);
        }
        return response()->json(['code' => '000', 'msg' => '保存内容成功']);
    }
    

}